import ipcRenderer from "../../ipcRenderer/ipcRenderer";
import { localStorage } from "../../request"
import { Message } from "element-ui";
import store from "../../store";

export default {
    name: "login-page",
    data() {
        return {
            inORup: false,
            userData: {
                userName: "",
                userEmail: "",
                passWord: ""
            }
        };
    },
    created() { },
    mounted() {

    },
    methods: {
        change_inORup() {
            this.userData = {
                userName: "",
                userEmail: "",
                passWord: ""
            };
            this.inORup = !this.inORup;
        },
        door(type) {
            if (type) {
                if (this.ifEmpty(this.userData)) {
                    this.login(this.userData);
                }
            } else {
                if (this.ifEmpty(this.userData)) {
                    if (this.isQualified(this.userData)) {
                        this.logup(this.userData);
                    }
                }
            }
        },
        // function 判断userData是否为空
        ifEmpty(userData) {
            if (userData.userEmail === "" || userData.passWord === "") {
                Message.warning("字段不能为空！");
                return false;
            }
            return true;
        },
        // function 判断是否符合规则
        isQualified(userData) {
            // Message.success("检测账号和密码是否合法");
            console.log(userData);
            let { userName, userEmail, passWord } = userData;

            // 用户名是否合法(字母开头，允许5-16字节，允许字母数字下划线)：^[a-zA-Z][a-zA-Z0-9_]{4,15}$
            let userNameReg = /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/;
            console.log("userName", userNameReg.test(userName));
            if (!userNameReg.test(userName)) {
                Message.warning("用户名：字母开头，允许5-16字节，允许字母数字下划线！");
                return false;
            }

            // Email地址：^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
            let userEmailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
            console.log("userEmail", userEmailReg.test(userEmail));
            if (!userEmailReg.test(userEmail)) {
                Message.warning("邮箱：格式错误！");
                return false;
            }

            // 密码(以字母开头，长度在6~18之间，只能包含字母、数字和下划线)：^[a-zA-Z]\w{5,17}$
            let passWordReg = /^[a-zA-Z]\w{5,17}$/;
            console.log("passWord", passWordReg.test(passWord));
            if (!passWordReg.test(passWord)) {
                Message.warning("密码：以字母开头，长度在6~18之间，只能包含字母、数字和下划线！");
                return false;
            }

            return true;
        },
        login(userData) {
            // console.log("登录");
            let { userEmail, passWord } = userData;
            localStorage.getItem("email:" + userEmail).then((res) => {
                if (passWord !== res.passWord) {
                    Message.warning("密码错误！");
                } else {
                    Message.success("登录成功！");
                    ipcRenderer.ipcrenderer(userData);
                    // this.init_indexedDB();
                    store.commit("save_currentEmail", userData.userEmail);
                    this.$router.push("/home/home2");
                }
            }).catch((err) => {
                Message.error("账号不存在，请先注册该账号！");
            });
        },
        logup(userData) {
            // console.log("注册", userData);
            localStorage.getItem("email:" + userData.userEmail).then((res) => {
                Message.warning("账号已存在，请重新注册账号！");
            }).catch((err) => {
                localStorage.setItem("email:" + userData.userEmail,
                    {
                        userName: userData.userName,
                        passWord: userData.passWord,
                        level: 0,
                        data: {
                            Favorite_Songs: [],
                            Favorite_SongsList: [],
                            Historical_Search: [],
                            Historical_Songs_List: [],
                        }
                    }).then(() => {
                        Message.success("注册成功，快去登录试试吧！");
                        this.change_inORup();
                    });
            });
        },

    },
};